$(function () {
    areas();
   /* var timeArrMoni=["08:00","08:30","09:00","09:30","10:00","10:30","11:00","11:30","12:00","12:30"];
    var timeArrAfter=["13:00","13:30","14:00","14:30","15:00","15:30","16:00","16:30","17:00","17:30"];
    var ageHtml="";
    for(var i=0;i<timeArrMoni.length;i++){
        ageHtml+="<option value='"+timeArrMoni[i]+"'>"+timeArrMoni[i]+"</option>"
    }
    $("#monringSele").append(ageHtml);
    $("#monringSele2").append(ageHtml);
    var ageHtml1="";
    for(var i=0;i<timeArrAfter.length;i++){
        ageHtml1+="<option value='"+timeArrAfter[i]+"'>"+timeArrAfter[i]+"</option>"
    }
    $("#afterSele").append(ageHtml1);
    $("#afterSele2").append(ageHtml1);
    var html1="";
    for(var k=1;k<31;k++){
        html1+="<option value="+k+">"+k+"</option>"
    }
    $("#dataWork").append(html1);*/
});
/*重置*/
function resetClick(){
    $("#severName").val("");
    getbaseList();
}
//树形省市区
function areas(){
    $.ajax({
        url : "/vaccinestation/queryTree",
        type : "post",
        success : function(data) {
            var zNodes=data.obj;
            //设置
            var setting = {view:{dblClickExpand: false,selectedMulti:false,nameIsHTML:true},data:{simpleData:{enable:true}},callback:{onClick:onClickTree}};
            $.fn.zTree.init($("#addrTreeDiv"), setting, zNodes);
        }
    })
    getbaseList();
}
/*地区点击事件*/
function onClickTree(e, treeId, treeNode) {
    var zTree = $.fn.zTree.getZTreeObj("addrTreeDiv"),
        nodes = zTree.getSelectedNodes(),v ="",n ="",p="",type="";
    for (var i=0, l=nodes.length; i<l; i++) {
        v += nodes[i].name + ",";//获取name值
        n += nodes[i].id + ",";//获取id值
        type += nodes[i].type + ",";//获取等级
        var pathNodes=nodes[i].getPath();
        for(var y=0;y<pathNodes.length;y++){
            p+=pathNodes[y].name+"/";//获取path/name值
        }
    }
    if (v.length > 0 ) v = v.substring(0, v.length-1);
    if (n.length > 0 ) n = n.substring(0, n.length-1);
    if (p.length > 0 ) p = p.substring(0, p.length-1);
    if (type.length > 0 ) type = type.substring(0, type.length-1);
    getbaseList(1,n,type);
}
/*查询*/
function getbaseList(init,areaId,type){
    JY.Model.loading();
	$("#ticktTable tbody").html("");
    var pageNum=$("#baseForm1 .pageNum").val();
    var pageSize=$("#baseForm1 .pageSize").val();
    var severName=$("#severName").val();
    var datas={};
    datas.pageNum=pageNum;
    datas.pageSize=pageSize;
    datas.stationName=severName;
    if(areaId!=undefined&&areaId!=null&&areaId!=""){
      if(type==1){
          datas.provinceAreaId=areaId;
      }else if(type==2){
          datas.cityAreaId=areaId;
      }else if(type==3){
          datas.countyAreaId=areaId;
      }
    }
	$.ajax({
		url : jypath + "/vaccinestation/queryVaccineStation",
		type : "post",
		data:datas,
		jsonType : "json",
		success : function(data) {
		    console.log(data)
            console.log("获取列表")
            var data=data instanceof Object ? data : JSON.parse(data);
            var html="";
            var list=data.obj;
            var results=list.results;
            var pageNum=list.pageNum,
                pageSize=list.pageSize,
                totalRecord=list.totalRecord;
           if(results!=null&&results.length>0){
                var leng=(pageNum-1)*pageSize;//计算序号
                for(var i = 0;i<results.length;i++){
                    var l=results[i];
                    var jsonL=JSON.stringify(l);
                    html+= "<tr class='abc'>" ;
                    html+="<td class='center ver-m'><label><input type='checkbox' name='ids' value="+jsonL+" class='ace' /> <span class='lbl'></span></label></td>";
                    html+="<td class='center'>"+l.stationName+"</td>";
                    html+="<td class='center'>"+l.fullName+" "+l.stationAddr+"</td>";
                    if(l.latitude==null||l.longitude==null||l.latitude==""||l.longitude==""){
                        html+="<td class='center'>暂无</td>";
                    }else{
                        html+="<td class='center'>"+l.latitude+","+l.longitude+"</td>";
                    }
                    html+="</tr>"
                }
                $("#ticktTable tbody").append(html);
               $("#pageing ul").empty();//清空分页
                JY.Page.setPage("baseForm1","pageing",pageSize,pageNum,totalRecord,"getbaseList");
            }else{
                html+="<tr><td colspan='4' class='center'>没有相关数据</td></tr>";
                $("#ticktTable tbody").append(html);
                $("#pageing ul").empty();//清空分页
            }
            JY.Model.loadingClose();
        }
	})
    getAllVaccine()
}
//获取所有疫苗
function getAllVaccine(){
    $.ajax({
        url : jypath + "/vaccine/queryVaccineType",
        type : "post",
        jsonType : "json",
        data:{
            "pageNum":1,
            "pageSize":1000,
        },
        success:function(data) {
            console.log(data);
            $("#ymAll").html("");
            if(data.obj.results!=null&&data.obj.results.length>0) {
                if (data.obj.results.length > 0) {
                    var html1="";
                    for (var i = 0; i < data.obj.results.length; i++) {
                        var l = data.obj.results[i];
                        html1+="<option value="+l.vaccineTypeId+">"+l.vaccineTypeName+"</option>"
                    }
                    $("#ymAll").append(html1);
                    $('#ymAll').selectpicker('refresh');
                    $('#ymAll').selectpicker('render');
                }
            }
        }
    })
    $("#distpicker1").distpicker();
}
//新增服务站
function add(){
    clearData();
    JY.Model.edit("auForm_addVaccine","新增服务站",function(){
        var that = $(this);
        if(JY.Validate.form("addVaccineForm")){
            $(".masks").removeClass("hide");
            var provinceId = $("#eprovinceName").find("option:selected").attr("data-code");
            var cityId = $("#ecityName").find("option:selected").attr("data-code");
            var districtId = $("#edistrictName").find("option:selected").attr("data-code");
            /*var times=[];
            if($("#monringCB").is(":checked")){
                var start=$("#monringSele option:selected").val();
                var end=$("#monringSele2 option:selected").val();
                if(start==""||end==""){
                    JY.Model.info("请完善时间段!");
                }else{
                    var time="上午/"+start+"-"+end;
                    times.push(time);
                }
            }
            if($("#afterCB").is(":checked")){
                var start1=$("#afterSele option:selected").val();
                var end1=$("#afterSele2 option:selected").val();
                if(start1==""||end1==""){
                    JY.Model.info("请完善时间段!");
                }else{
                    var time="下午/"+start1+"-"+end1;
                    times.push(time);
                }
            }
            var dataWork=$("#dataWork").val();
            var timeWork="";
            var timeData="";
            if(dataWork!=null){
                timeWork=dataWork.join(",");
            }
            if(times.length>0){
                timeData=times.join(",");
            }*/
            if(provinceId==""){
                JY.Model.info("请选择服务站地区!");
            }
            /*else if(dataWork==null){
                JY.Model.info("请选择每月工作日!");
            }else if(times.length==0){
                JY.Model.info("请完善上班时间段!");
            }*/
            else{
                $.ajax({
                    url : jypath + "/vaccinestation/insertVaccineStation",
                    type : "post",
                    data:{
                        "stationName":$("#addVaccineForm input[name='station_name']").val().trim(),
                        "phone":$("#addVaccineForm input[name='phone']").val().trim(),
                        "provinceAreaId":provinceId,
                        "cityAreaId":cityId,
                        "countyAreaId":districtId,
                        "stationAddr":$("#addVaccineForm input[name='station_addr']").val().trim(),
                        "longitude":$("#addVaccineForm input[name='longitude']").val().trim(),
                        "latitude":$("#addVaccineForm input[name='latitude']").val().trim(),
                        "stationDes":$("#addVaccineForm textarea[name='station_des']").val(),
                        "stationTime":$("#addVaccineForm textarea[name='stationTimeDes']").val(),
                        "serviceIntegral":$("#addVaccineForm input[name='intearge']").val().trim(),
                        "orderDay":$("#addVaccineForm input[name='dayNum']").val().trim(),
                        "sign":$("#jurisdiction").find("option:selected").val(),
                        /*"WorkTime":timeWork,
                        "time":timeData,*/
                    },
                    jsonType : "json",
                    success : function(data) {
                        if(data.res==1){
                            that.dialog("close");
                            $(".masks").addClass("hide");
                            JY.Model.info("操作成功!");
                            getbaseList();
                        }
                    }
                })
            }
        }
    });
}
//编辑
function edit(){
    clearData();
    var chks =[];
    $('#ticktTable input[name="ids"]:checked').each(function(){
        chks.push($(this).val());
    });
    if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length==1){
        var obj=JSON.parse(chks);
        $("#auForm_addVaccine input[name='station_name']").val(obj.stationName);
        $("#auForm_addVaccine input[name='phone']").val(obj.phone);
        $("#auForm_addVaccine textarea[name='station_des']").val(obj.stationDes);
        $("#auForm_addVaccine input[name='station_addr']").val(obj.stationAddr);
        $("#auForm_addVaccine textarea[name='stationTimeDes']").val(obj.stationTime);
        /* var dataWork=h.workTime;
         var arr=[];
         if(dataWork!=null){
             arr=dataWork.split(",");
         }
         $('#dataWork').selectpicker('val', arr).trigger("change");*/
        $("#eprovinceName").find("option[data-code = '"+obj.provinceAreaId+"']").prop("selected","selected");
        $("#eprovinceName").trigger("change");
        $("#ecityName").find("option[data-code = '"+obj.cityAreaId+"']").prop("selected","selected");
        $("#ecityName").trigger("change");
        $("#edistrictName").find("option[data-code = '"+obj.countyAreaId+"']").prop("selected","selected");
        $("#auForm_addVaccine input[name='longitude']").val(obj.longitude);
        $("#auForm_addVaccine input[name='latitude']").val(obj.latitude);
        if(obj.serviceIntegral==null||obj.serviceIntegral==undefined){
            obj.serviceIntegral="";
        }
        if(obj.orderDay==null||obj.orderDay=="0"||obj.orderDay==undefined||obj.orderDay==0){
            obj.orderDay="";
        }
        $("#auForm_addVaccine input[name='intearge']").val(obj.serviceIntegral);
        $("#auForm_addVaccine input[name='dayNum']").val(obj.orderDay);
        $("#jurisdiction").find("option[value = '"+obj.sign+"']").prop("selected","selected");
        /*var vaccineTimes=h.vaccineTimes;
        var time1=vaccineTimes.split("/");
        if(time1[0]=="上午"){
            $("#monringCB").prop("checked",true);
            var time2=time1[1].split("-");
            $("#monringSele").find("option[value = '"+time2[0]+"']").prop("selected","selected");
            $("#monringSele2").find("option[value = '"+time2[1]+"']").prop("selected","selected");
        }else if(time1[0]=="下午"){
            $("#afterCB").prop("checked",true);
            var time2=time1[1].split("-");
            $("#afterSele").find("option[value = '"+time2[0]+"']").prop("selected","selected");
            $("#afterSele2").find("option[value = '"+time2[1]+"']").prop("selected","selected");
        }*/
        JY.Model.edit("auForm_addVaccine","编辑服务站",function(){
            var that = $(this);
            var provinceId = $("#eprovinceName").find("option:selected").attr("data-code");
            var cityId = $("#ecityName").find("option:selected").attr("data-code");
            var districtId = $("#edistrictName").find("option:selected").attr("data-code");
            if(JY.Validate.form("addVaccineForm")){
                $.ajax({
                    url : jypath + "/vaccinestation/updateVaccineStation",
                    type : "post",
                    data:{
                        "stationId":obj.stationId,
                        "stationName":$("#addVaccineForm input[name='station_name']").val().trim(),
                        "phone":$("#addVaccineForm input[name='phone']").val().trim(),
                        "provinceAreaId":provinceId,
                        "cityAreaId":cityId,
                        "countyAreaId":districtId,
                        "stationAddr":$("#addVaccineForm input[name='station_addr']").val().trim(),
                        "longitude":$("#addVaccineForm input[name='longitude']").val().trim(),
                        "latitude":$("#addVaccineForm input[name='latitude']").val().trim(),
                        "stationDes":$("#addVaccineForm textarea[name='station_des']").val(),
                        "stationTime":$("#addVaccineForm textarea[name='stationTimeDes']").val(),
                        "serviceIntegral":$("#addVaccineForm input[name='intearge']").val().trim(),
                        "orderDay":$("#addVaccineForm input[name='dayNum']").val().trim(),
                        "sign":$("#jurisdiction").find("option:selected").val(),
                        /*"WorkTime":timeWork,
                        "time":timeData,*/
                    },
                    jsonType : "json",
                    success : function(data) {
                        if(data.res==1){
                            that.dialog("close");
                            JY.Model.info("操作成功!");
                            getbaseList();
                        }
                    }
                })
            }
        });
    }else if(chks.length>1){
        JY.Model.info("一次只能编辑一条信息!");
    }
}
//配置疫苗
function addAgent(){
    var chks =[];
    $('#ticktTable input[name="ids"]:checked').each(function(){
        chks.push($(this).val());
    });
    if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length==1){
        var obj=JSON.parse(chks);
        var masks="";
        //查询疫苗
        var arr=[];
        document.getElementById("ymAll").options.selectedIndex = 0;
        $("#ymAll").selectpicker('refresh');
        $('.selectpicker').selectpicker('val', '').trigger("change");
        $.ajax({
            url : jypath + "/stationVaccine/getVaccineTypeByStationId",
            type : "get",
            data:{
                "stationId":obj.stationId,
            },
            jsonType : "json",
            success : function(data) {
                if(data.obj!=null&&data.obj.length>0){
                    masks=1;//编辑
                    for(var i=0;i<data.obj.length;i++){
                        arr.push(data.obj[i].vaccineTypeId);
                    }
                    $('.selectpicker').selectpicker('val', arr).trigger("change");
                }else{
                    masks=0;//新增
                    $('.selectpicker').selectpicker('val','').trigger("change");
                }
            }
        })
        JY.Model.edit("abForm","配置疫苗",function(){
            var that=$(this);
            var carouselsArr= $('#ymAll').val();
            console.log(carouselsArr);
            var vaccineTypeId=carouselsArr.join(",");
            if(carouselsArr==null||carouselsArr.length==0){
                JY.Model.info("请先配置相关疫苗!");
            }else{
                if(masks==0){
                     //新增
                    $.ajax({
                        url : jypath + "/stationVaccine/insertYmStationVaccineBatch",
                        type : "post",
                        data:{
                            "stationId":obj.stationId,
                            "vaccineTypeId":vaccineTypeId,
                        },
                        jsonType : "json",
                        success : function(data) {
                            that.dialog("close");
                            if(data.res==1){
                                JY.Model.info("操作成功!");
                                getbaseList();
                            }
                        }
                    })
                }else{
                     //编辑
                    $.ajax({
                        url : jypath + "/stationVaccine/updateByPrimaryKeySelective",
                        type : "post",
                        data:{
                            "stationId":obj.stationId,
                            "list":vaccineTypeId,
                        },
                        jsonType : "json",
                        success : function(data) {
                            that.dialog("close");
                            if(data.res==1){
                                JY.Model.info("操作成功!");
                                getbaseList();
                            }
                        }
                    })
                }

            }
        });
    }else if(chks.length>1){
        JY.Model.info("一次只能编辑一条信息!");
    }
}
function clearData(){
    $("#auForm_addVaccine input").val("");
    $("#auForm_addVaccine textarea").val("");
    // $('#dataWork').selectpicker('val', '').trigger("change");
    // $("#monringSele").find("option[value = '']").prop("selected","selected");
    // $("#monringSele2").find("option[value = '']").prop("selected","selected");
    // $("#afterSele").find("option[value = '']").prop("selected","selected");
    // $("#afterSele2").find("option[value = '']").prop("selected","selected");
    $("#eprovinceName").val("");
    $("#jurisdiction").find("option[value = '1']").prop("selected","selected");
    $("#ecityName").val("");
    $("#edistrictName").val("");
}
